<!DOCTYPE html>
<html>
<head>
	<title>05-command</title>
	<script type="text/javascript" src="js/mummy.js" charset="utf-8"></script>
	
</head>

<body>

<script type="text/javascript" charset="utf-8">
//d'après l'ex du livre "Design Patterns - Tête la 1ère"

Class({$define:function Lampe(){
	this.Name;
	this.Lampe = function(name){this.Name = name;};
	this.marche = function(){console.log('Lampe '+this.Name+' : Marche');};
	this.arret = function(){console.log('Lampe '+this.Name+' : Arrêt');};
	
}});


Interface(
	function ICommande(){
		this.executer = function(){};
	}
);

Class({$implements:[ICommande],$define:function CommandeAllumerLampe(){
		this.Lampe;
		
		this.CommandeAllumerLampe = function(lampe){
			if($(lampe).Is(Lampe)){
				this.Lampe=lampe;
			}else{throw 'Lampe expected';}
		};
		
		this.executer = function(){
			this.Lampe.marche();
		};
		
	}
});

Class({$implements:[ICommande],$define:function CommandeEteindreLampe(){
		this.Lampe;
		
		this.CommandeEteindreLampe = function(lampe){
			if($(lampe).Is(Lampe)){
				this.Lampe=lampe;
			}else{throw 'Lampe expected';}
		};
		
		this.executer = function(){
			this.Lampe.arret();
		};
		
	}
});

Class({$define:function Telecommande(){
	this.CommandesMarche = [];
	this.CommandesArret = [];
	
	this.Telecommande=function(){
		this.CommandesMarche = new Array();
		this.CommandesMarche = new Array();
	};
	
	this.setCommande=function(num,cmdMarche,cmdArret){
		if($(cmdMarche).Implements(ICommande)){
			if($(cmdArret).Implements(ICommande)){
				this.CommandesMarche[num] = cmdMarche;
				this.CommandesArret[num] = cmdArret;
				
			}else{throw 'ICommande expected / cmdArret';}
		}else{throw 'ICommande expected / cmdMarche';}

	};
	
	this.Allumer=function(num){this.CommandesMarche[num].executer();};
	this.Eteindre=function(num){this.CommandesArret[num].executer();};
}
	
});

var lampeSejour = $(Lampe).getNew("Séjour");
var lampeCuisine = $(Lampe).getNew("Cuisine");
var lampeChambre = $(Lampe).getNew("Chambre");

var telecommande = $(Telecommande).getNew();

telecommande.setCommande(0,$(CommandeAllumerLampe).getNew(lampeSejour),
							$(CommandeEteindreLampe).getNew(lampeSejour));

telecommande.setCommande(1,$(CommandeAllumerLampe).getNew(lampeCuisine),
							$(CommandeEteindreLampe).getNew(lampeCuisine));

telecommande.setCommande(2,$(CommandeAllumerLampe).getNew(lampeChambre),
							$(CommandeEteindreLampe).getNew(lampeChambre));

telecommande.Allumer(0);
telecommande.Allumer(1);
telecommande.Allumer(2);

telecommande.Eteindre(0);
telecommande.Eteindre(1);
telecommande.Eteindre(2);
</script>
	
	

</body>
</html>
